import json
import requests
from langchain.agents import Tool
from agent.agent_selector import get_access_token


def travel_assistant(user_input):
    system_text = "You are a virtual travel assistant that can generate personalized travel plans based on user preferences. Structure: Please ensure that there is a blank line between different paragraphs to clearly separate them. If the user says please give me x days, the daily plan should be greater than 120 words Summary: Each paragraph should start with a concluding word to indicate the main point, but do not use Markdown format to emphasize it. Please reply in English only."
    url = f"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token={get_access_token()}"
    payload = json.dumps({
        "messages": [{"role": "user", "content": user_input}],
        # 是否打开联网搜索
        "disable_search": True,
        "system": system_text
    })
    headers = {'Content-Type': 'application/json'}
    response = requests.post(url, headers=headers, data=payload)
    return response.json().get("result", "无返回结果")


travel_assistant_tool = Tool(
    name="Virtual Travel Assistant",
    func=travel_assistant,
    description="生成个性化旅行计划。"
)
